package tgc.edu.ljc.layui.web.controller;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.ljc.layui.custom.DataGridParam;
import tgc.edu.ljc.layui.custom.DataGridUtils;
import tgc.edu.ljc.layui.custom.LayUiResult;
import tgc.edu.ljc.layui.entity.Stu;
import tgc.edu.ljc.layui.entity.SysRole;
import tgc.edu.ljc.layui.entity.TbClass;
import tgc.edu.ljc.layui.service.ClassService;
import tgc.edu.ljc.layui.service.RoleService;
import tgc.edu.ljc.layui.service.StuService;
import tgc.edu.ljc.layui.web.form.StuForm;
@Controller
@RequestMapping(value="/student")
public class StuController {
	@Autowired
	private StuService service;
	@Autowired
	private ClassService classService;
	@Autowired
	private RoleService roleService;
	
	@RequestMapping(value="/manage")
	public String manage(ModelMap map) {
		List<TbClass> classes = classService.findAll();
		map.put("classes", classes);
		return "student/manage";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(DataGridParam param,Integer classId) {
		Pageable pageable = param.getPabeable();
		Page<Stu> page=null;
		if(classId==null) {
			classId=0;
		}
		page=service.findByTbClassId(classId,pageable);
		return DataGridUtils.buildResult(page);
	}
	
	@RequestMapping(value="/edit")
	public String edit(Integer classId,Integer id,ModelMap map) {
		Stu model=new Stu();
		if(id!=null) {
			model=service.findById(id);
		}else if(classId!=null) {
			TbClass bj=classService.findById(classId);
			model.setTbClass(bj);
		}
		map.put("model", model);
		return "student/edit";
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(StuForm form) {
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		Integer id = form.getId();
		Stu model=new Stu();
		if(id==null) {
			SysRole role = roleService.findById(2);
			model.getRoles().add(role);
		}else {
			model=service.findById(id);
		}
		if(!StringUtils.hasText(model.getPassword())) {
			model.setPassword(encoder.encode("1111"));
		}
		String password = form.getPassword();
		if(password!=null) {
			model.setPassword(encoder.encode(password));
		}
		BeanUtils.copyProperties(form, model,"id","roles","password");
		try {
			service.save(model);
			return LayUiResult.getResult("数据保存成功");
		} catch (Exception e) {
			return LayUiResult.getErrorResult("数据保存失败！");
		}
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(Integer id) {
		try {
			service.deleteById(id);
			return LayUiResult.getResult("数据删除成功！");
		} catch (Exception e) {
			return LayUiResult.getErrorResult("数据删除失败！");
		}
	}
	
	@RequestMapping(value="/classChange")
	@ResponseBody
	public Object classChange(Integer classId) {
		List<Stu> stus=service.findByTbClassId(classId);
		return stus;
	}
}
